package net.mcg_server.jbi.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

import net.mcg_server.jbi.Program;
import net.mcg_server.jbi.data.Game;
import net.mcg_server.jbi.gui.menu.MenuBar;

public class AddGame {

	// -------------
	// VARIABLES
	// -------------
	private JFrame frame;
	private JPanel panel;
	
	private JTextField titleField;
	private JTextArea descriptionField;
	private JCheckBox isBeatenCheckBox;
	private JLabel titleLabel;
	private JLabel descriptionLabel;
	private JLabel isBeatenLabel;
	private JButton addButton;
	private Program program;

	public AddGame(Program program) {
		initialize();
		frame.setVisible(true);
		this.program = program;
	}

	private void initialize() {
		// create the frame
		frame = new JFrame("Add A Game");
		frame.setBounds(250, 200, 400, 215);
		frame.toFront();
		frame.setResizable(false);
		
		// create the panel
		panel = new JPanel();
		panel.setLayout(null);

		// add panel to frame
		frame.add(panel);
		
		//Title Label
		titleLabel = new JLabel("* Title: ");
		panel.add(titleLabel);		
		
		//Title Text Field
		titleField = new JTextField();
		panel.add(titleField);

		//Description Label
		descriptionLabel = new JLabel("* Description: ");
		panel.add(descriptionLabel);

		//Description Text Field
		descriptionField = new JTextArea();
		panel.add(descriptionField);
		
		//isBeaten Label
		isBeatenLabel = new JLabel("* Is Beaten? ");
		panel.add(isBeatenLabel);
		
		//isBeaten CheckBox
		isBeatenCheckBox = new JCheckBox();
		panel.add(isBeatenCheckBox);
		
		//Add Game button
		addButton = new JButton("Add Game");
		panel.add(addButton);
		
		//Insets, Dimensions, Placements, and Borders
		Insets insets = panel.getInsets();
		Border bGrayLine = BorderFactory.createLineBorder(Color.GRAY);
		
		Dimension size = titleLabel.getPreferredSize();
		titleLabel.setBounds(30+insets.left, 20+insets.top, size.width, size.height);
		
		size = titleField.getPreferredSize();
		titleField.setBounds(120+insets.left, 20+insets.top, 250, 20);
		titleField.setBorder(bGrayLine);
		
		size = descriptionLabel.getPreferredSize();
		descriptionLabel.setBounds(30+insets.left, 50+insets.top, size.width, size.height);
		
		size = descriptionField.getPreferredSize();
		descriptionField.setBounds(120+insets.left, 50+insets.top, 250, 50);
		descriptionField.setBorder(bGrayLine);
		
		size = isBeatenLabel.getPreferredSize();
		isBeatenLabel.setBounds(30+insets.left, 120+insets.top, size.width, size.height);
		
		size = isBeatenCheckBox.getPreferredSize();
		isBeatenCheckBox.setBounds(120+insets.left, 120+insets.top, size.width, size.height);
		
		size = addButton.getPreferredSize();
		addButton.setBounds(120+insets.left, 150+insets.top, 160, size.height);
		
		//------------
		// LISTENERS
		//------------
		
		// Button Clicked
		addButton.addMouseListener(new MouseListener(){
			@Override
			public void mouseClicked(MouseEvent e){
				
				if((!titleField.getText().equals((String) "") && 
						!descriptionField.getText().equals((String) ""))){
					
					Game game = new Game();
					game.setTitle(titleField.getText());
					game.setDescription(descriptionField.getText());
					//System.out.println("Description: " + game.getDescription());
					game.setBeaten(isBeatenCheckBox.isSelected());
					
					program.getGameList().add(game);
					program.getMainPanel().loadData();
					
					MenuBar.menuOpened = false;
					program.getMainFrame().requestFocus();
					frame.dispose();
					
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
		});
		
		// close AccountManageWindow
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (true) {
					MenuBar.menuOpened = false;
					program.getMainFrame().requestFocus();
					frame.dispose();
				}
			}
		});
		
		// return focus
		frame.addWindowFocusListener(new WindowAdapter(){
			@Override
			public void windowLostFocus(WindowEvent e){
				frame.requestFocus();
			}
		});
	}
}
